﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace WABingTranslateAPI.Tool
{
    public class WebTool
    {
        public static string UrlDecode(string urlToDecode)
        {
            string result;
            if (string.IsNullOrEmpty(urlToDecode))
            {
                result = urlToDecode;
            }
            else
            {
                result = HttpUtility.UrlDecode(urlToDecode, Encoding.UTF8);
            }
            return result;
        }

        /// <summary>
        /// 下载字符串
        /// </summary>
        /// <param name="url"></param>
        /// <param name="pathSave"></param>
        /// <returns></returns>
        public static string DownloadString(string url)
        {
            try
            {
                if (!string.IsNullOrEmpty(url))
                {
                    WebClient client = new WebClient();

                    client.Headers.Add("Accept: */*");
                    client.Headers.Add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET4.0C; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X MetaSr 1.0)");
                    client.Headers.Add("Accept-Language: zh-cn");
                    client.Headers.Add("Content-Type: multipart/form-data");
                    //client.Headers.Add("Accept-Encoding: gzip, deflate");
                    client.Headers.Add("Cache-Control: no-cache");


                    string URLAddress = url;

                    //client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
                    return client.DownloadString(URLAddress);


                }
            }
            catch (Exception ee)
            {

                //WebTools.ConsoleLine(ee.Message);
                //throw;
            }
            return null;

        }


    }
}